/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { SmsMonitorSendSms } from '@/services/yz/smsMonitor';
import { Drawer, Form, Select, Input, Button, message } from 'antd';
import React, { useState } from 'react';

const { TextArea } = Input;

const tempalte = {
  first:
    '尊敬的快宝用户：您好！\n由于您的驿站投诉较多，投诉单号如下#DH#，请您入库及时通知客户，并做好服务，减少投诉，以免影响使用！',
  second:
    '尊敬的快宝用户：您好！\n由于您的驿站投诉率非常高，投诉单号如下#DH#，收到此通知后三日内进行整改完毕，否则该驿站将被禁止使用！',
};

const SmsTemplateDrawer: React.FC<{ data?: Record<string, string>; ids?: React.Key[] }> = (
  props,
) => {
  const { data: { phone = '', cm_id = '' } = {}, ids } = props;
  const [visible, setVisible] = useState<boolean>(false);
  const [form] = Form.useForm();
  const onClose = () => {
    setVisible(false);
  };
  const onOpen = () => {
    setVisible(true);
  };

  const requestTableData = async (values: Record<string, string>) => {
    const { content } = values;
    if (!content) {
      message.error('请输入模块内容');
      return;
    }
    const { code, msg } = await SmsMonitorSendSms({
      phone: phone,
      content: content,
      cm_id: ids ? ids.toString() : cm_id,
    });
    if (code == 0) {
      message.info(msg);
    } else {
      message.error(msg);
    }
    setVisible(false);
  };

  const onValuesChange = (values: { select: any }) => {
    if (values.select) {
      form.setFieldsValue({ content: tempalte[values.select] });
    }
  };

  return (
    <>
      <div onClick={onOpen}>{props.children}</div>
      <Drawer visible={visible} onClose={onClose} destroyOnClose title={'推送单号推送通知'}>
        <Form form={form} onFinish={requestTableData} onValuesChange={onValuesChange}>
          <Form.Item name={'select'} initialValue='first'>
            <Select
              options={[
                { label: '模版1', value: 'first' },
                { label: '模版2', value: 'second' },
              ]}
            />
          </Form.Item>
          <Form.Item name={'content'} initialValue={tempalte.first}>
            <TextArea autoSize={{ minRows: 6 }} />
          </Form.Item>
          <Button type='primary' htmlType='submit'>
            确定
          </Button>
        </Form>
      </Drawer>
    </>
  );
};

export default SmsTemplateDrawer;
